package info.wangyuan.springbootdemo.controller;


import info.wangyuan.springbootdemo.entity.Book;
import info.wangyuan.springbootdemo.mapper.BookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 书籍控制层
 *
 * @author Albert
 * @since 2025-10-30 16:56:27
 */
@Controller
public class BookController {

    @Autowired
    private BookMapper bookMapper;

    /**
     * 数据校验
     */
    private String checkData(String title, String author, Double price) {
        if (title.isBlank()) {
            return "书籍名称不能为空";
        }
        if (author.isBlank()) {
            return "作者不能为空";
        }
        if (price == null) {
            return "价格不能为空";
        }
        if (price <= 0) {
            return "价格必须是正整数";
        }
        return null;
    }

    /**
     * 保存书籍
     */
    @ResponseBody
    @RequestMapping("/book/save")
    public String saveBook(@RequestParam String title,
                           @RequestParam String author,
                           @RequestParam Double price) {
        // 1.数据校验
        String checkResult = checkData(title, author, price);
        if (checkResult != null) {
            return checkResult;
        }

        // 2.实体对象封装
        Book book = new Book();
        book.setTitle(title);
        book.setAuthor(author);
        book.setPrice(price);

        // 3.通过数据访问层操作数据库，保存书籍
        bookMapper.save(book);

        // 4.返回结果
        return "保存成功";
    }

    /**
     * 查询书籍
     */
    @ResponseBody
    @RequestMapping("/book/get/{id}")
    public String getBook(@PathVariable Integer id) {
        // 1.数据校验
        if (id == null) {
            return "id不能为空";
        }

        // 2.通过数据访问层操作数据库，查询书籍
        Book book = bookMapper.getById(id);

        // 3.返回结果
        return "书籍名称：" + book.getTitle() + "，作者：" + book.getAuthor() + "，价格：" + book.getPrice();
    }

    /**
     * 修改书籍
     */
    @ResponseBody
    @RequestMapping("/book/update")
    public String updateBook(@RequestParam Integer id,
                             @RequestParam String title,
                             @RequestParam String author,
                             @RequestParam Double price) {
        // 1.判断数据库中是否存在书籍数据
        Book dbBook = bookMapper.getById(id);
        if (dbBook == null) {
            return "书籍不存在";
        }

        // 2.数据校验
        String checkResult = checkData(title, author, price);
        if (checkResult != null) {
            return checkResult;
        }

        // 3.实体对象封装
        Book book = new Book();
        book.setId(id);
        book.setTitle(title);
        book.setAuthor(author);
        book.setPrice(price);

        // 4.通过数据访问层操作数据库，修改书籍
        bookMapper.updateById(book);

        // 5.返回结果
        return "修改成功";
    }

    /**
     * 删除书籍
     */
    @ResponseBody
    @RequestMapping("/book/delete/{id}")
    public String deleteBook(@PathVariable Integer id) {
        // 1.数据校验
        if (id == null) {
            return "id 不能为空";
        }

        // 2.通过数据访问层操作数据库，删除书籍
        bookMapper.deleteById(id);

        // 3.返回结果
        return "删除成功";
    }

}
